# -*- coding: utf-8 -*-

'''
@author: Gil Duarte 6424
@date: 01/12/2014
@version final delivery
'''

from readfile import *
from sqlalchemy import *
import matplotlib.pyplot as plot
from matplotlib import *

class Stats():
	def __init__(self):
		db = create_engine ('sqlite:///IPC_Portugal_1977_2013b.db')
		DBSession = sessionmaker(bind = db)
		self.session = DBSession()
		self.xAxisLabel = 'Years'
		self.yAxisLabel = { "IPC Global", "Variacao anual taxa %",  "Rem. Min. Mensal €", "Rem. Max. Mensal €",  "PIB €", "Rend. Nac. Bruto €",  "Rend. Disp. P.A €",  "Remun. P.A. €"}
						
	def getData (datatype, initialYear = 1977, finalYear = 2013):
		collectedData = []
		
		for x in range (initialYear, finalYear):
			search = self.session.query(Data).filter(Data.ano == x)
			returnedValues = ''
			if datatype == "ipc":
				returnedValues = search.ipc_global_1981_100
			elif datatype == "variacao_anual_taxa":
				returnedValues = search.taxa_de_inflacao
			elif datatype == "rem_min":
				returnedValues == search.remuneracao_minima_mensal
			elif datatype == "rem_max":
				returnedValues == search.remuneracao_maxima_mensal
			elif datatype ==  "pib":
				returnedValues == search.pib_per_capita_anual
			elif datatype == "ren_nacional":
				returnedValues == search.rendimento_nacional_bruto_lper_capita
			elif datatype ==  "ren_disp":
				returnedValues == search.rendimento_disponivel_bruto_per_capita
			elif datatype ==  "remuneracoes":
				returnedValues == search.remuneracoes_per_capita
			elif datatype == "":
				print "Operation not permited"
			else:
				pass
		return data
	
	def createStatsGraph (self, datatype, initialYear, finalYear):
		searchedYears = initialYear, finalYear
		data = self.getData (datatype, initialYear, finalYear)
		figure = plot.figure()
		ax = figure.add_subplbot (111)
		ax.plot (searchedYears, data)
		ax.set_title (datatype)
		ax.set_ylabel (self.yAxisLabel[datatype])
		ax.set_xlabel (self.xAxisLabel)
		plot.grid (True)
		plot.draw()
		plot.show()
		return figure



